<?php


namespace Modules\Admin\Tests\Feature;


use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Activitylog\Models\Activity;
use Tests\AdminTestCase;

class InfoControllerTest extends AdminTestCase {
    use RefreshDatabase;

    public function testAdmins() {
        $this->as()->put('api/admin/admins/1', ['name' => 'aaa'])->dump();
        dd(Activity::query()->get()->toArray());
    }

    public function testGetInto() {
        $response = $this->getJson(route('api.admin.info'));
        $response->assertStatus(401);
        $response = $this->actingAs($this->admin, 'admin')->getJson(route('api.admin.info'));
//        $response->dump();
        $response->assertStatus(200)->assertJson([
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'username' => $this->admin->username,
                'name' => $this->admin->name,
                'id' => $this->admin->id,
                'created_at' => $this->admin->created_at,
                'updated_at' => $this->admin->updated_at,
            ],
        ])->assertJsonStructure([
            'data' => ['roles'],
        ]);
    }

    public function testUpdateInfo() {

        $url = route('api.admin.info.update');
        //未登录情况下
        $response = $this->putJson($url);
        $response->assertStatus(401);
        //已登录 未传数据
        $response = $this->actingAs($this->admin, 'admin')->putJson($url, ['username' => '', 'name' => '']);

        $response->assertStatus(422)->assertJson(['code' => 0])
            ->assertJsonStructure(['msg'])
            ->assertJsonValidationErrors([
                'username',
                'name',
            ]);
        //正常情况下
        $response = $this->putJson($url,
            [
                'username' => 'admin1',
                'name' => 'admin1',
            ]);
        $response->dump();
        $response->assertStatus(200)->assertJson(['code' => 0, 'msg' => 'success', 'data' => [
            'username' => 'admin1',
            'name' => 'admin1',
            'id' => $this->admin->id,
            'created_at' => $this->admin->created_at,
        ]])->assertJsonStructure(['data' => ['updated_at']]);
    }

    public function testUpdateInfoPassword() {
        $url = route('api.admin.info.password.update');
        //未登录情况下
        // $response = $this->putJson($url);
        // $response->assertStatus(401);
        //未传字段情况
        // $response = $this->actingAs($this->admin, 'admin')->putJson($url);
        // $response->assertStatus(422)->assertJson(['code' => 0, 'msg' => '参数验证失败'])
        //     ->assertJsonValidationErrors([
        //         'old_password', 'new_password', 'new_password_confirmation'
        //     ]);
        //
        // //两次密码不一致情况
        // $response = $this->actingAs($this->admin, 'admin')->putJson($url,
        //     [
        //         'old_password'              => 'admin12',
        //         'new_password'              => 'admin123',
        //         'new_password_confirmation' => 'admin1234'
        //     ]);
        // $response->assertStatus(422)->assertJson(['code' => 0, 'msg' => '参数验证失败'])
        //     ->assertJsonValidationErrors([
        //         'new_password'
        //     ]);
        // //原始密码错误情况
        // $response = $this->actingAs($this->admin, 'admin')->putJson($url,
        //     [
        //         'old_password'              => 'admin321',
        //         'new_password'              => 'admin1',
        //         'new_password_confirmation' => 'admin1'
        //     ]);
        // [$code, $message] = CodeResponse::OLD_PASSWORD_FAIL;
        // $response->assertStatus(200)->assertJson(['code' => $code, 'msg' => $message]);

        //数据正确情况
        $response = $this->as()->putJson($url . '?' . http_build_query(['aaa' => 123]),
            [
                'old_password' => 'admin123',
                'new_password' => 'admin1',
                'new_password_confirmation' => 'admin1',
            ], [
                'cccc' => 'aaa',
            ]);
        $response->assertStatus(200)->assertJson(['code' => 0, 'msg' => 'success'])->dump();
        // $this->admin->update(['name'=>'aaaaa']);
        dd(Activity::query()->get()->toArray());
    }
}
